package com.itheima.reggie.mapper;

import com.itheima.reggie.entity.Address;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Service;

import java.util.List;

@Mapper
public interface AddressMapper {

    //获取地址数据
    @Select("SELECT * FROM address_book where user_id = #{userId};")
    List<Address> list(Long userId);

    //添加地址
    @Insert("INSERT INTO address_book( id , user_id , consignee , sex , phone , detail , label  , is_default , create_time , update_time , create_user , update_user) VALUES ( null , #{userId} , #{consignee} , #{sex} , #{phone} , #{detail} , #{label}  , #{isDefault} , #{createTime} , #{updateTime} , #{createUser} , #{updateUser} );")
    void add(Address address);

    //将该用户所有的默认值设置为0
    @Update("UPDATE address_book SET is_default = 0  WHERE user_id = #{userId};")
    void updateDefault1(Long userId);

    @Update("UPDATE address_book SET is_default = 1  WHERE id = #{addressId};")
    void updateDefault2( Long  addressId);

    @Select("SELECT * FROM address_book where id = #{id};")
    Address findById(Long id);

    //查询默认地址
    @Select("SELECT * FROM address_book where user_id = #{userId} and is_default = 1 ;")
    Address findDefault(Long userId);
}
